package com.restaurant.web.android.service;

import java.awt.print.PageFormat;
import java.awt.print.Paper;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.util.ArrayList;
import java.util.Date;

import javax.print.PrintService;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.restaurant.domain.Employeecredential;
import com.restaurant.domain.Menuorder;
import com.restaurant.domain.Tableorder;
import com.restaurant.domain.Tables;
import com.restaurant.domain.Tableshape;
import com.restaurant.service.MenuorderService;
import com.restaurant.service.TableorderService;
import com.restaurant.web.android.utill.PrintToKitchen;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;

@Component
@RequestMapping("/menuorderservice.do")
public class MenuorderController {
	
	@Autowired
	private MenuorderService objMenuorderService;
	
	@Autowired
	private TableorderService obTableorderService;
	
	@RequestMapping(method = RequestMethod.GET)
	public void menuorderControllerGETRequest(HttpServletRequest request, HttpServletResponse response){
		Long menuorderID = new Long(Long.parseLong(request.getParameter("menuorderID")));
		try {
			objMenuorderService.deleteByPrimaryKey(menuorderID);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping(method = RequestMethod.POST)
	public void menuorderControllerPOSTRequest(HttpServletRequest request, HttpServletResponse response){
		XStream objXStream = new XStream(new DomDriver());
		objXStream.alias("Tables", Tables.class);
        objXStream.alias("Tableshape", Tableshape.class);
        objXStream.alias("Employeecredential", Employeecredential.class);
        objXStream.alias("Menuorder", Menuorder.class);
        objXStream.alias("ArrayList", ArrayList.class);
        objXStream.alias("Tableorder", Tableorder.class);
		
		@SuppressWarnings("unchecked")
		ArrayList<Menuorder> listMenuorders = (ArrayList<Menuorder>) objXStream.fromXML(request.getParameter("ArrayList"));
		Tables tables = (Tables) objXStream.fromXML(request.getParameter("Tables"));
		Employeecredential employeecredential = (Employeecredential) objXStream.fromXML(request.getParameter("Employeecredential"));
		Tableorder tableorder = (Tableorder) objXStream.fromXML(request.getParameter("Tableorder"));
		boolean printToKitchen = (Boolean) objXStream.fromXML(request.getParameter("printToKitchen"));
		
		
		tableorder.setStarttime(new Date());
		tableorder.setTables(tables);
		tableorder.setEmployee(employeecredential.getEmployee());
		
		try {
			tableorder = obTableorderService.addOrModify(tableorder);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		for(int i=0;i< listMenuorders.size();i++){
			try {
				listMenuorders.get(i).setTableorder(tableorder);
				listMenuorders.set(i, objMenuorderService.addOrModify(listMenuorders.get(i)));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(printToKitchen){
			PrintToKitchen objPrintToKitchen = new PrintToKitchen(listMenuorders, tableorder);
			PrinterJob job = PrinterJob.getPrinterJob();
			PageFormat pageFormat = new PageFormat();
		    Paper paper = new Paper();
		    double margin = 20;
		    paper.setImageableArea(margin, margin, 3*72, 11*72);
		    pageFormat.setPaper(paper);
	        job.setPrintable(objPrintToKitchen, pageFormat);
	        PrintService[] printService = PrinterJob.lookupPrintServices();
	        
	        try {
	            job.setPrintService(printService[0]);
	            job.print();
	        } catch (PrinterException ex) {
	            ex.printStackTrace();
	        }
		}
	}
	
}
